package com.vsoft.sms_cm;
import java.net.UnknownHostException;
import java.sql.Connection;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

import org.apache.log4j.Logger;
import org.apache.log4j.PropertyConfigurator;

import com.vsoft.anti_fra.TestQuery;
import com.vsoft.framework.core.BusiObj;
import com.vsoft.framework.core.Configuration;
import com.vsoft.framework.server.servlet.SessionData;
import com.mongodb.Mongo;
import com.mongodb.DB;
import com.mongodb.DBCollection;
import com.mongodb.BasicDBObject;
import com.mongodb.DBObject;
import com.mongodb.DBCursor;
import com.mongodb.MongoException;

import com.vsoft.framework.core.BaseException;
import com.vsoft.framework.core.BusiObj;
import com.vsoft.framework.core.Configuration;
import com.vsoft.framework.core.db.DBGadgets;
import com.vsoft.framework.core.db.DBTrans;
import com.vsoft.framework.core.db.OperAgent;
import com.vsoft.framework.core.db.SQLArgs;
import com.vsoft.framework.core.esb.ServiceBus;
import com.vsoft.framework.core.util.BOHelper;
import com.vsoft.framework.core.util.LogCollect;
import com.vsoft.framework.core.util.XMLUtil;
import com.vsoft.framework.server.servlet.SessionData;
import com.vsoft.framework.server.servlet.HttpUtil;
public class testCacheMongodb {
	private static Logger vLogger = Logger.getLogger(testCacheMongodb.class);
	private static String vName = testCacheMongodb.class.getName();
	private SessionData vSD = null;
	private String vDebugHome = "";
	private BusiObj				vInBO		=  new BusiObj();
	private final static String		RESULT			="RESULT";
	public static void main(String[] args) throws BaseException  
	{
	
		//1 get ip dbName
		String 	ip				=	args[0];
		String	dbName			=	args[1];
		//PropertyConfigurator.configure("log4j.properties");
		HashMap<String,String> dbConfig	= new HashMap<String,String>();

		try {
			//2 init
			Configuration.InitSystem();
			Logger vLogger = Logger.getLogger(testCacheMongodb.class);
			dbConfig.put("IP", ip);
			dbConfig.put("DBNAME", dbName);
			
			//3 connection
			Connection conn = DBTrans.getConnection();

			Mongodb	mdb		=	new Mongodb();
			Mongo	mongo			=	mdb.getMongodb(dbConfig);
			DB	db			=	mdb.getDB(mongo, dbName);
			vLogger.info("---------------InitSystem-------------");
			long start_time=System.currentTimeMillis();
			vLogger.info("---------------start-------------"+start_time);
			vLogger.info("=========1、查询数据库=======");
			String 	condition = " and rownum=1 ";
			String sql 					=	TableCacheServerRedis.genSqlTriggerPhone(condition);
			SQLArgs 	Arg			= 	new SQLArgs();
			Arg.reset();
			vLogger.info("initialize cache (1)query smsTemple \n sql:" +sql);
			BusiObj		smsTempleBo 			= OperAgent.execQuery(conn, "getSubscriberAndSMSTemple", sql, Arg);
			//处理短信模板，key=被叫号码，即触发电话
			ArrayList<BusiObj> alSms				=	smsTempleBo.getBusiObjList(RESULT);
			String order_num	= "";
			vLogger.info("=========插入mongodb=======");
			String collectionName	=	"cache";
			DBCollection coll = db.getCollection(collectionName);
			//coll.remove(o);
			for(BusiObj m:alSms)
			{
				order_num = m.getString("order_num");
				vLogger.info("=========bo======="+m.getRawData());
				BasicDBObject doc = new BasicDBObject();
				HashMap<String, Object>  hData = m.getRawData();
				doc.put(order_num, hData);
				coll.insert(doc);


/*				for(Map.Entry <String,   Object>   entry   :   hData.entrySet())   
				{ 
					System.out.println(entry.getKey()   +   "   -->   "   +   entry.getValue()); 
					
				}*/
/*				ArrayList<BusiObj> AlBo			=	vData.get(order_num);
				if(AlBo==null)
					AlBo										=	new	ArrayList<BusiObj>();
				//m	=	TableCacheServerRedis.formatAlarmContext(m);
				AlBo.add(m);
				vData.put(order_num, AlBo);*/
			}
			vLogger.info("\n====================init put vData over====================\n");

/*			vLogger.info("---------------start-------------"+start_time);
			long	count		=	10;//1w
			String collection = "testInsert";
			mdb.insertSample(db, collection, count);
			
			long batch_time=System.currentTimeMillis();
			vLogger.info("---------------insert 1w-------------"+(batch_time-start_time));
			count				=	1000000;//100w
			mdb.insertSample(db, collection, count);
			
			batch_time=System.currentTimeMillis();
			vLogger.info("---------------insert 100w-------------"+(batch_time-start_time));*/
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	public void  init()
	{
		
	}

}
